import UIAbility from '@ohos.app.ability.UIAbility'
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
import Logger from '../util/Logger'

const TAG: string = 'EntryAbility'

export default class EntryAbility extends UIAbility {
  async onCreate(want, launchParam) {
    Logger.info(TAG, `gobangtest EntryAbility onCreate`)
    let deviceFlag = want.parameters.isB
    if (deviceFlag) {
      Logger.info(TAG, `gobangtest EntryAbility deviceFlag = ${JSON.stringify(deviceFlag)}`)
      AppStorage.SetOrCreate('deviceFlag', deviceFlag)
    }
    Logger.info(TAG, 'deviceFlag = ' + deviceFlag)
    let atManager = abilityAccessCtrl.createAtManager()
    try {
      await atManager.requestPermissionsFromUser(this.context, ['ohos.permission.DISTRIBUTED_DATASYNC']).then((data) => {
        Logger.info(TAG, `data: ${JSON.stringify(data)}`)
      }).catch((err) => {
        Logger.info(TAG, `err: ${JSON.stringify(err)}`)
      })
    } catch (err) {
      Logger.info(TAG, `catch err->${JSON.stringify(err)}`)
    }
  }

  onDestroy() {
    Logger.info(TAG, `EntryAbility onDestroy`)
  }

  onWindowStageCreate(windowStage) {
    Logger.info(TAG, `EntryAbility onWindowStageCreate`)
    windowStage.setUIContent(this.context, 'pages/Index', null)
  }

  onWindowStageDestroy() {
    Logger.info(TAG, `EntryAbility onWindowStageDestroy`)
  }

  onForeground() {
    Logger.info(TAG, `EntryAbility onForeground`)
  }

  onBackground() {
    Logger.info(TAG, `EntryAbility onBackground`)
  }
}